<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1_ref</title>
</head>

<body>

    <div id="test" />
    <script type="text/javascript"
        src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/react/18.2.0/umd/react.production.min.js"></script>
    <script type="text/javascript"
        src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
    <script type="text/javascript"
        src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/babel-standalone/6.26.0/babel.min.js"></script>

    <script type="text/babel">
        //  回调函数的ref, ref在react中会自动帮忙调用,,把所处的节点挂到了实例身上, 调用次数
        class Demo extends React.Component {
            state = {isHot: true}

            // 展示左侧输入框是数据
            showData = () => {
                const { input1 } = this
                alert(input1.value)
            }

            changeWeather = ()=> {
                const {isHot} = this.state
                this.setState({isHot: !isHot})
            }

            // ref的绑定函数
            saveInput = (c)=> {
                this.input1 = c; 
                console.log('@@', c)
            }
            
            render() {
                const {isHot} = this.state
                return (
                    <div>
                        <h2>今天的天气很{isHot?'炎热':'凉爽'}</h2>
                        {/**
                            1.第一次调用render的时候,当写一个函数式ref时,react会自动调用ref把当前节点绑定到当前对象实例中
                            console.log会自动执行,, 内联函数,在更新时会被执行2次,,
                        */}
                         {/**<input re={c => {this.input1 = c; console.log('@', c)}} type="text" placeholder="点击按钮提示数据" /> */}
                         <input ref={this.saveInput}  type="text" placeholder="点击按钮提示数据"/>
                        <button onClick={this.showData}>点击提示左侧的数据</button>
                        <button onClick={this.changeWeather}>点我切换天气</button>
                    </div>
                )
            }
        }
        ReactDOM.render(<Demo a="1" />, document.getElementById("test"))
    </script>
</body>

</body>

</html>